package com.combetter.changxing.ui.fragment;

import android.bluetooth.BluetoothGatt;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;

import com.combetter.changxing.MyApp;
import com.combetter.changxing.R;
import com.combetter.changxing.bluetooth.FacilityClient2;
import com.combetter.changxing.databinding.FragmentMineBinding;
import com.combetter.changxing.db.entity.User;
import com.combetter.changxing.model.EventMessage;
import com.combetter.changxing.ui.base.BaseFragment;
import com.combetter.changxing.ui.dialog.BelowSelectDialog;
import com.combetter.changxing.viewmodel.MineViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static com.combetter.changxing.bluetooth.FacilityClient2.AUTH_SUCCESS;
import static com.combetter.changxing.constanst.Constants.REUEST_CODDE;

/**
 * 我的Fragment
 * <p>
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/7.
 */
public class MineFragment extends BaseFragment<FragmentMineBinding, MineViewModel> {
    private String temp1 = "0℃";
    private String temp2 = "0";

    @Override
    public int setLayout() {
        EventBus.getDefault().register(this);
        return R.layout.fragment_mine;
    }

    @Override
    public void init() {
        mBinding.setUser(MyApp.getInstance().getUserInfo());
    }

    @Override
    public Class createViewModel() {
        return MineViewModel.class;
    }

    @Override
    public void loadData() {
        //获取用户信息
        mViewModel.getUserInfo();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MyApp.getInstance().getUserInfo() != null) {
            mBinding.setUser(MyApp.getInstance().getUserInfo());
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (FacilityClient2.mConnectState == BluetoothGatt.STATE_CONNECTED) {
                mBinding.tvVersion.setText(TextUtils.isEmpty(MyApp.getInstance().getUserInfo().getFacilityVersion()) ? "" : MyApp.getInstance().getUserInfo().getFacilityVersion());

                new Handler().postDelayed(() -> {
                    if (FacilityClient2.getClient().mAuthStatu == AUTH_SUCCESS) {
                        FacilityClient2.getClient().getVersion();
                    }
                }, 500);

            } else {
                mBinding.tvVersion.setText("");
            }
            User user = MyApp.getInstance().getUserInfo();
            if (user != null) {
                mViewModel.temperature.set(TextUtils.isEmpty(user.getTemperatureTrimming()) ? "0℃" : user.getTemperatureTrimming());
                mViewModel.preheat.set(TextUtils.isEmpty(user.getTimeTrimming()) ? "0" : user.getTimeTrimming());
            } else {
                mViewModel.temperature.set("0℃");
                mViewModel.preheat.set("0");
            }

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REUEST_CODDE) {
            String stringExtra = data.getStringExtra(BelowSelectDialog.REQUESE);
            int titleId = data.getIntExtra(BelowSelectDialog.REQUESEID, 0);
            if (titleId == getViewModel().temperatureId) {
                temp1 = stringExtra;
                mViewModel.setTemperature(stringExtra);
            } else if (titleId == getViewModel().warmId) {
//                mViewModel.warm.set(stringExtra);
////                mViewModel.setWarm();
            } else if (titleId == getViewModel().preheatId) {
                temp2 = stringExtra;
                mViewModel.setPreheat(stringExtra);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEventData(EventMessage message) {
        switch (message.getType()) {
            case "CMD_SEND_FEVER"://温度
                setValue(true);
                break;
            case "CMD_PREHEAT_TIME"://时间
                setValue(false);
                break;
            case "onDisConnected":
                mViewModel.temperature.set("0℃");
                mViewModel.preheat.set("0");
                mBinding.tvVersion.setText("");
                break;
            case "BleSuccess":
            case "Version":
                if (FacilityClient2.mConnectState == BluetoothGatt.STATE_CONNECTED) {
                    mBinding.tvVersion.setText(TextUtils.isEmpty(MyApp.getInstance().getUserInfo().getFacilityVersion()) ? "" : MyApp.getInstance().getUserInfo().getFacilityVersion());
                } else {
                    mBinding.tvVersion.setText("");
                }
                break;
        }
    }

    /**
     * 设值
     *
     * @param flag
     */
    private void setValue(boolean flag) {
        User user = MyApp.getInstance().getUserInfo();
        if (user == null) {
            mViewModel.temperature.set("0℃");
            mViewModel.preheat.set("0");
            mBinding.tvVersion.setText("");
            return;
        }
        if (user != null && flag) {
            user.setTemperatureTrimming(temp1);
            mViewModel.temperature.set(temp1);
        } else if (user != null && !flag) {
            mViewModel.preheat.set(temp2);
            user.setTimeTrimming(temp2);
        }
        if (user != null) {
            MyApp.getInstance().getDatabase().userDao().updateUser(user);
        }
    }
}
